<?php


namespace App\Http\Controllers\Admin;


use App\Models\Admin;
use App\Models\Shop;
use App\Transformers\ShopTransformer;
use Dingo\Api\Http\Request;

class ShopController extends BaseController
{

    public function index() {
        $users = Shop::paginate();
        return $this->response->paginator($users, new ShopTransformer([]));
    }

    public function store(Request $request) {
        $input = $request->input();
        $this->validate($request, [
            'phone'   => 'required|numeric|regex:/^1[3456789][0-9]{9}$/|unique:admins',
            'username'  => 'required|min:4|max:14|unique:admins',
            'password'  => 'required|confirmed|min:6|max:14',
            'password_confirmation' => 'required|same:password'
        ]);
        $validator =  \Validator::make($input, [
            'phone'   => 'required|numeric|regex:/^1[3456789][0-9]{9}$/|unique:admins',
            'username'  => 'required|min:4|max:14|unique:admins',
            'password'  => 'required|confirmed|min:6|max:14',
            'password_confirmation' => 'required|same:password'
        ], [
            'phone.unique' => '手机号已存在',
            'phone.required' => '手机号为空'
        ]);
        if ($validator->fails()) {
            $error = $validator->errors()->first();
            return $this->failed($error);
        }
        $input['password'] = bcrypt($input['password']);
        if ($admin = Admin::create($input)){
            return $this->success(['id' => $admin->id]);
        }
        return $this->failed('创建失败');
    }


}

